function varargout = changeDetectionUI(varargin)
% OPEN MATLAB code for Open.fig
%      OPEN, by itself, creates a new OPEN or raises the existing
%      singleton*.
%
%      H = OPEN returns the handle to a new OPEN or the handle to
%      the existing singleton*.
%
%      OPEN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in OPEN.M with the given input arguments.
%
%      OPEN('Property','Value',...) creates a new OPEN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Open_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Open_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Open

% Last Modified by GUIDE v2.5 19-Jun-2015 21:22:32

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @changeDetectionUI_OpeningFcn, ...
                   'gui_OutputFcn',  @changeDetectionUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Open is made visible.
function changeDetectionUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Open (see VARARGIN)

% Choose default command line output for Open
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Open wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = changeDetectionUI_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in open1.
function open1_Callback(hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    [FileName1, PathName1] = uigetfile({'*.*';'.hdf';'.HDR'},...
        'Select ENVI/MODIS/MERIS data','Inputs/Sardegna/Pre_img');

    if ~isempty(FileName1)
        set(handles.fileaddress1,'String', [PathName1 FileName1]);
    else
        set(handles.fileaddress1,'String', 'DATA ERROR!');
    end
catch
    set(handles.fileaddress1,'String', 'NOT ENVI/MODIS/MERIS DATA!');
end

% --- Executes on button press in open2.
function open2_Callback(hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

try
    [FileName2, PathName2] = uigetfile({'*.*';'.hdf';'.HDR'},...
        'Select ENVI/MODIS/MERIS data','Inputs/Sardegna/Pre_img');

    if ~isempty(FileName2)
        set(handles.fileaddress2,'String', [PathName2 FileName2]);
    else
        set(handles.fileaddress2,'String', 'DATA ERROR!');
    end
catch
    set(handles.fileaddress2,'String', 'NOT ENVI/MODIS/MERIS DATA!');
end


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over open1.
function open1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over open2.
function open2_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to open2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in startCD.
function startCD_Callback(hObject, eventdata, handles)
% hObject    handle to startCD (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
imgAdr1 = get(handles.fileaddress1, 'String');
imgAdr2 = get(handles.fileaddress2, 'String');

if ((~(strcmp(imgAdr1, 'Time 1 Image Address')) && ~(strcmp(imgAdr1, 'DATA ERROR!')) && ~(strcmp(imgAdr1, 'NOT ENVI/MODIS/MERIS DATA!')))...
        && (~(strcmp(imgAdr1, 'Time 2 Image Address')) && ~(strcmp(imgAdr2, 'DATA ERROR!')) && ~(strcmp(imgAdr2, 'NOT ENVI/MODIS/MERIS DATA!')))...
        && (get(handles.manualRadio, 'value') || get(handles.autoRadio, 'value')))
        
    if get(handles.manualRadio, 'value')
        changeDetection(imgAdr1, imgAdr2, 'Manual');
    elseif get(handles.autoRadio, 'value')
        switch get(handles.methodList, 'value')
            case 1
                changeDetection(imgAdr1, imgAdr2, 'Kmeans');
            case 2
                changeDetection(imgAdr1, imgAdr2, 'Otsu');
            case 3
                changeDetection(imgAdr1, imgAdr2, 'KI');
        end
    end
end

% --- Executes during object creation, after setting all properties.
function open1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function open2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to open2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function fileaddress1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fileaddress1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
if ispc && isequal(get(hObject, 'BackgroundColor'), get(0, 'defaultUicontrolBackgroundColor'))
    set(hObject, 'BackgroundColor', 'white');
end


% --- Executes during object creation, after setting all properties.
function fileaddress2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fileaddress2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
if ispc && isequal(get(hObject, 'BackgroundColor'), get(0, 'defaultUicontrolBackgroundColor'))
    set(hObject, 'BackgroundColor', 'white');
end


% --- Executes on selection change in methodList.
function methodList_Callback(hObject, eventdata, handles)
% hObject    handle to methodList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns methodList contents as cell array
%        contents{get(hObject,'Value')} returns selected item from methodList


% --- Executes during object creation, after setting all properties.
function methodList_CreateFcn(hObject, eventdata, handles)
% hObject    handle to methodList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in manualRadio.
function manualRadio_Callback(hObject, eventdata, handles)
% hObject    handle to manualRadio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of manualRadio
set(handles.manualRadio, 'value', 1);
set(handles.autoRadio, 'value', 0);

% --- Executes on button press in autoRadio.
function autoRadio_Callback(hObject, eventdata, handles)
% hObject    handle to autoRadio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of autoRadio
set(handles.manualRadio, 'value', 0);
set(handles.autoRadio, 'value', 1);
